﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data.Entity;
namespace DAL
{
    /// <summary>
    /// 关键字DAL层
    /// </summary>
    public class DAL_CMS_Keyword
    {
        //查询所有的热评关键字数据以及条件查询评论
        //分页查询                                                 当前页数     每页几条     关键字
        public static Pager<CMS_Keyword> GetCMS_Keyword(int PageIndex, int PageSize, string keyword)
        {
            using (var db = new KQCMS())
            {
                //List<Where> where = new List<Where>();
                //总条数  
                var count = db.CMS_Keyword.Count() /*% PageSize == 0 ? db.View_CommentAU.Count() / PageSize : db.View_CommentAU.Count() / PageSize + 1*/;
                //所有的数据
                var list = db.CMS_Keyword.OrderByDescending(x => x.ltimes).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
                //关键字
                if (!string.IsNullOrEmpty(keyword))
                {
                    count = db.CMS_Keyword.Where(x => x.keyword.Contains(keyword)).Count();
                    list = list.Where(x => x.keyword.Contains(keyword)).ToList();
                }
                Pager<CMS_Keyword> p = new Pager<CMS_Keyword>();
                p.Count = count;
                p.Data = list;
                return p;
            }
        }
        /// <summary>
        /// 添加关键字
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public static int AddCMS_Keyword(CMS_Keyword kd)
        {
            using (var db = new KQCMS())
            {
                kd.ltimes = DateTime.Now;
                db.Entry(kd).State = EntityState.Added;

                if (db.SaveChanges() > 0)
                    return 1;
                else
                    return 0;
            }
        }

        //根据id删除关键字
        public static int DeleteID(int kid)
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Keyword.Where(x => x.kid == kid).FirstOrDefault();
                db.Entry(list).State = EntityState.Deleted;
                if (db.SaveChanges() > 0)
                    return 1;
                else
                    return 0;
            }
        }
        /// <summary>
        /// 根据kid查询数据 
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static CMS_Keyword SelectID(int kid)
        {
            using (var db = new KQCMS())
            {
                CMS_Keyword cm = db.CMS_Keyword.Where(x => x.kid == kid).FirstOrDefault();
                //var list = db.CMS_Keyword.Where(x => x.kid == kid).ToList();
                return cm;
            }
        }
        /// <summary>
        /// 根据传来的对象进行修改
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public static int UpdateCMS_Keyword(CMS_Keyword kd)
        {
            using (var db = new KQCMS())
            {
                db.Entry(kd).State = EntityState.Modified;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 查询前五条热评关键字
        /// </summary>
        /// <returns></returns>
        public static List<CMS_Keyword> SelectKeywords()
        {
            using (var db = new KQCMS())
            {
                return db.CMS_Keyword.OrderByDescending(x => x.stimes).Take(5).ToList();
            }
        }


    }
}
